package com.petalsgroup.petals.web.servlet.animation;

import com.petalsgroup.petals.pojo.DiscussDO;
import com.petalsgroup.petals.pojo.DiscussVO;
import com.petalsgroup.petals.pojo.ResultInfo;
import com.petalsgroup.petals.pojo.UserDO;
import com.petalsgroup.petals.service.discuss.DiscussService;
import com.petalsgroup.petals.service.discuss.impl.DiscussServiceImpl;
import com.petalsgroup.petals.web.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@WebServlet("/discuss/*")
public class DiscussServlet extends BaseServlet {
    private DiscussService discussService = new DiscussServiceImpl();

    /**
     * 添加讨论
     * @author
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void addDiscuss(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        DiscussDO discuss = new DiscussDO();
        String parameter = req.getParameter("aid");
        discuss.setAid(Integer.valueOf(parameter));
        UserDO user = (UserDO) req.getSession().getAttribute("user");
        discuss.setUid(user.getUid());
        discuss.setCreate_time(new Date());
        parameter = req.getParameter("pre");
        if (parameter == null || parameter.equals("")) {
            discuss.setPre_id(0);
        } else {
            discuss.setPre_id(Integer.valueOf(parameter));
        }
        parameter = req.getParameter("context");
        discuss.setContext(parameter);
        boolean flag = discussService.insertDiscuss(discuss);
        ResultInfo info = new ResultInfo();
        info.setFlag(flag);
        writeValue(resp, info);
    }

    /**
     * 获取某部动漫的讨论
     * @author
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void getAllDiscuss(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String aid = req.getParameter("aid");
        List<DiscussVO> list = discussService.listDiscussByAid(Integer.parseInt(aid));
        ResultInfo info = new ResultInfo();
        info.setData(list);
        writeValue(resp, info);
    }

    /**
     * 查看回复
     * @author
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void getReply(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pre = req.getParameter("pre");
        List<DiscussVO> list = discussService.listDiscussByPreId(Integer.parseInt(pre));
        ResultInfo info = new ResultInfo();
        info.setData(list);
        writeValue(resp, info);
    }
}
